import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class Test {
    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();
        binaryTree.createTree();
     /*   binaryTree.preOrder(binaryTree.root);
        System.out.println( );
        System.out.print(binaryTree.size(binaryTree.root));
        System.out.println(binaryTree.isSymmetric(binaryTree.root));
        binaryTree.levelOrder2(binaryTree.root);
        System.out.println(binaryTree.levelOrder3(binaryTree.root));*/
        System.out.println(binaryTree.tree2str(binaryTree.root));
    }
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if (root==null){
            return list;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur=root;
        while (cur!=null||!stack.empty()) {
            while (cur != null) {
                stack.push(cur);
                System.out.println(cur.val + " ");
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            cur = top.right;
        }
        return list;
    }
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if (root==null){
            return list;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur=root;

        while (cur!=null||!stack.empty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            TreeNode top = stack.pop();
            System.out.println(top.val+" ");
            cur = top.right;
        }
        return list;
    }
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        if(root==null){
            return list;
        }
        Stack<TreeNode> ret = new Stack<>();
        TreeNode cur = root;
        TreeNode prev = null;
        while(cur!=null||!ret.empty()) {
            while (cur != null) {
                ret.push(cur);
                cur = cur.left;
            }
            TreeNode top = ret.peek();
            if (top.right == null || top.right == prev) {
                ret.pop();
                list.add((int) top.val);
                prev = top;
            } else {
                cur = top.right;
            }
        }
        return list;
    }
}
